import request from '@/utils/request'
import axios from 'axios'

export function getWordInfoMore(word) {
    const config = {
        method: 'get',
        url: 'https://v2.xxapi.cn/api/englishwords?word=' + word,
    };

    return axios(config)
        .then(function (response) {
            return response.data;
        });
}
//例句展示
export function getWordSentence(data) {
    let sentencesHtml = ''
    for (let i = 0; i < data.length; i++) {
        sentencesHtml += '<div>' + data[i]['s_content'] + '---' + data[i]['s_cn'] +  '</>'
    }
    if (data.length === 0) {
        sentencesHtml = '<p style="color: red">没有例句</p>'
    }
    // console.log(sentencesHtml)
    return sentencesHtml
}

//单词扩展释义
export function getMessageBoxContent(data) {
    let title = "<strong style='color: #7a6df0'>单词分类---<i style='color: #7a6df0'>" + data.bookId + "</i></strong><br>"
    let title2 = "<p><strong style='color: #00afff;'>英音音标---<i style='color: #00afff'>[" + data.ukphone + "]</i></strong>"
    let title3 = "<strong style='color: #1ab394;margin-left: 10px'>美音音标---<i style='color: #1ab394'>[" + data.usphone + "]</i></strong></p>"
    let phrases = data.phrases
    let translations = data.translations
    const translationsTitle = '<div class="card-header"><strong style="color: red">翻译</strong></div>'
    let translationsHtml = ""
    for (let i = 0; i < translations.length; i++) {
        translationsHtml +=  '<p>' + translations[i]['pos'] + "---" + translations[i]['tran_cn'] + '</p>'
    }
    const interval1 = '<div class="card-header"><strong style="color: #E6A23C">短语</strong></div>'
    let phrasesHtml = ''
    if (phrases && phrases.length === 0) {
        phrasesHtml = "------------------------------"
    }
    for (let i = 0; i < phrases.length; i++) {
        phrasesHtml += "<div class='text item'>" + phrases[i]['p_content'] + "---" + phrases[i]['p_cn'] + "</div>"
    }
    const interval2 = '<div class="card-header"><strong style="color: #67C23A">同根词</strong></div>'
    let relWords = data.relWords
    let relWordsHtml = ''
    if (relWords && relWords.length === 0) {
        relWordsHtml = '------------------------------'
    }
    for (let i = 0; i < relWords.length; i++) {
        let relWordElement = relWords[i]['Hwds'];
        let relWordElementHtml = ''
        for (let j = 0; j < relWordElement.length; j++) {
            relWordElementHtml += '<p>' + relWordElement[j]['hwd'] + "---" + relWordElement[j]['tran'] + '</p>'
        }
        relWordsHtml += "<div class='text item'>" + relWords[i]['Pos'] + ":" + relWordElementHtml + "</div>"
    }
    const interval3 = '<div class="card-header"><strong style="color: #4AB7BD">近义词</strong></div>'
    let synonyms = data.synonyms

    let synonymsHtml = ''
    if (synonyms && synonyms.length === 0) {
        synonymsHtml = '------------------------------'
    }
    for (let i = 0; i < synonyms.length; i++) {
        let synonymElement = synonyms[i]['Hwds'];
        let synonymElementHtml = ''
        for (let j = 0; j < synonymElement.length; j++) {
            synonymElementHtml += synonymElement[j]['word'] + " , "
        }
        synonymsHtml += "<div class='text item'>" + synonyms[i]['pos']  + ":" + '<p>'
            + synonymElementHtml.substring(0,synonymElementHtml.length - 3 ) + "---" + synonyms[i]['tran']  + '</p>' + "</div>"
    }

    let showMore = '<button class="dynamic-btn"  style="float: right;margin-top: 10px" >查看更多</button>'
    return "<div ref='container'>" + title2 + title3 + translationsTitle + translationsHtml +
        interval1 + phrasesHtml + interval2 + relWordsHtml + interval3 + synonymsHtml + "</div>";
}
/**
 * 保存全部测试记录
 * */
export function insertDataToNewRecord(params) {
    return request({
        url: "/word/insertList",
        method: "post",
        data: params
    })
}

export function getWordList(params) {
    return request({
        url: "/word/list",
        method: "get",
        params: params
    })
}

export function getPageData(params) {
    return request({
        url: "/word/listPage",
        method: "get",
        params: params
    })
}

export function getPageUnit(params) {
    return request({
        url: "/word/listUnit",
        method: "get",
        params: params
    })
}

export function getPageUnitCheck(params) {
    return request({
        url: "/word/listUnitCheck",
        method: "get",
        params: params
    })
}

export function getPageUnitTimes(params) {
    return request({
        url: "/word/listUnitTimes",
        method: "get",
        params: params
    })
}

export function addWordTest(params) {
    return request({
        url: "/word/addTest",
        method: "post",
        data: params
    })
}

export function putTestList(params) {
    return request({
        url: "/word/putTestList",
        method: "post",
        data: params
    })
}

export function getTestDetailsList(params) {
    return request({
        url: "/word/getTestDetailsList",
        method: "get",
        params: params
    })
}

export function getWriteDetailsList(params) {
    return request({
        url: "/word/getWriteDetailsList",
        method: "get",
        params: params
    })
}

export function writeDetailsList(params) {
    return request({
        url: "/word/writeDetailsList",
        method: "get",
        params: params
    })
}

export function putTestData(params) {
    return request({
        url: "/word/putTestData",
        method: "post",
        data: params
    })
}

export function putStudyData(params) {
    return request({
        url: "/word/putStudyData",
        method: "post",
        data: params
    })
}

export function putChinToEngData(params) {
    return request({
        url: "/word/chinToEngData",
        method: "post",
        data: params
    })
}

export function putDataCommon(params) {
    return request({
        url: "/word/insertOneDataCommon",
        method: "post",
        data: params
    })
}

export function putStudyList(params) {
    return request({
        url: "/word/putStudyList",
        method: "post",
        data: params
    })
}

export function listTestPage(params) {
    return request({
        url: "/word/listTestPage",
        method: "get",
        params: params
    })
}

export function listStudyPage(params) {
    return request({
        url: "/word/listStudyPage",
        method: "get",
        params: params
    })
}

export function removeTest(id) {
    return request({
        url: "/word/removeTest/" + id,
        method: "delete",
    })
}

// 查询单词听力记录列表
export function listHear(params) {
    return request({
        url: "/word/hear/getList",
        method: "get",
        params: params
    })
}

// 查询单词听力记录列表
export function listHearByGroup(params) {
    return request({
        url: "/word/hear/getListByGroup",
        method: "get",
        params: params
    })
}

// 保存单词听力详情
export function putHear(data) {
    return request({
        url: "/word/hear/putData",
        method: "post",
        data: data
    })
}

export function getHearOne(id) {
    return request({
        url: "/word/hear/getOne/" + id,
        method: "get",
    })
}

// 修改单词听力详情
export function updateHear(data) {
    return request({
        url: "/word/hear/updateData",
        method: "post",
        data: data
    })
}

// 查询单词听力详情记录列表
export function listHearRecordDetail(query) {
    return request({
        url: '/word/hearDetailList',
        method: 'get',
        params: query
    })
}

// 查询单词听力详情记录列表
export function listHearRecordDetailByUser(query) {
    return request({
        url: '/word/hearDetailListByUser',
        method: 'get',
        params: query
    })
}

//初高中单词学习记录详情
export function listStudyRecordDetailByUser(query) {
    return request({
        url: '/word/getStudyDetailsListByUser',
        method: 'get',
        params: query
    })
}

export function listTestRecordDetailByUser(query) {
    return request({
        url: '/word/getTestDetailsListByUser',
        method: 'get',
        params: query
    })
}

export function handleColumnAudio(row, rate) {
    if (rate === undefined || rate === null) {
        rate = 0.7
    }
    window.speechSynthesis.resume();
    const speech = new SpeechSynthesisUtterance();
    speech.text = row.name;
    speech.lang = "en-US";
    speech.volume = 1;
    speech.rate = rate;
    speech.pitch = 1;
    window.speechSynthesis.speak(speech);
}

